package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.dto.CommentEssayDTO;
import com.baizhi.shane.shane.dto.CommentQuestionDTO;
import com.baizhi.shane.shane.vo.CommentEssayVO;
import com.baizhi.shane.shane.vo.CommentQuestionVO;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baizhi.shane.sys.vo.SuccessedOutVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.entity.ShaneCommentEssay;
import com.baizhi.shane.shane.service.ShaneCommentEssayService;
import com.baizhi.shane.shane.mapper.ShaneCommentEssayMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author wbz12
* @description 针对表【shane_comment_essay(啥呢文章评论表)】的数据库操作Service实现
* @createDate 2023-07-25 22:33:37
*/
@Service
public class ShaneCommentEssayServiceImpl extends ServiceImpl<ShaneCommentEssayMapper, ShaneCommentEssay>
    implements ShaneCommentEssayService{

    @Resource
    private ShaneCommentEssayMapper shaneCommentEssayMapper;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 获取指定文章下所有的评论
     *
     * @Author 闻柏智
     * @Date 16:19 2024/2/28
     * @Param essayId
     * @return java.util.List<com.baizhi.shane.shane.vo.CommentEssayVO>
     **/
    @Override
    public List<CommentEssayVO> getAllCommentByEssayId(Long essayId) {
        // 从数据库获取数据
        List<CommentEssayDTO> shaneCommentEssayList = shaneCommentEssayMapper.getAllCommentByEssayId(essayId);

        // 返回值列表
        List<CommentEssayVO> commentEssayVOList = new ArrayList<>();
        /* 构造commentEssayVOList作为返回值 */
        for (CommentEssayDTO commentEssayDTO : shaneCommentEssayList) {
            /* 对一级评论做处理 */
            if (commentEssayDTO.getCommentLevel().equals(1L)) {
                /* 复制除子评论外的所有属性 */
                CommentEssayVO commentLevelOne = new CommentEssayVO();
                BeanUtils.copyProperties(commentEssayDTO, commentLevelOne);
                /* 构造children */
                List<CommentEssayVO> children = shaneCommentEssayList
                        .stream()
                        .filter(commentTwo -> commentTwo.getCommentLevel().equals(2L) && commentTwo.getParentId().equals(commentEssayDTO.getId() ) )
                        .map(commentTwo -> {
                            CommentEssayVO commentLevelTwo = new CommentEssayVO();
                            BeanUtils.copyProperties(commentTwo, commentLevelTwo);
                            return commentLevelTwo;
                        })
                        .collect(Collectors.toList());
                commentLevelOne.setChildren(children);
                commentEssayVOList.add(commentLevelOne);
            }
        }

        return commentEssayVOList;
    }

    /**
     * 新增一个评论
     *
     * @Author 闻柏智
     * @Date 16:22 2024/2/28
     * @Param shaneCommentEssay
     * @return com.baizhi.shane.sys.vo.SuccessedOutVO
     **/
    @Override
    public SuccessedOutVO insertOneComment(ShaneCommentEssay shaneCommentEssay) {
        boolean save = this.save(shaneCommentEssay);
        SuccessedOutVO successedOutVO =
                commonUtils.boolToSuccessedOutVO(save, SysMapping.INSERT_COMMENT_SUCCESS, SysMapping.INSERT_COMMENT_FAIL);
        return successedOutVO;
    }
}




